<template>
	<view class="main">
		<image @longpress="saveImg" :src='qrCode' style="width: 500upx;height: 500upx;"></image>
		<view style="text-align: center;padding: 10px;color: #CCC;font-size: 26upx;">长按二维码保存到相册</view>
	</view>
</template>

<script>
	export default{
		data(){
			return {
				qrCode:'https://qiuzhi.lalaluowang.club/sys/kefuweixin.jpg'
			}
		},
		methods:{
			saveImg:function(){
				let that=this
				    wx.getSetting({
				      success(res) {
				        //未授权 先授权 然后保存
				        if (!res.authSetting['scope.writePhotosAlbum']) {
				          wx.authorize({
				            scope: 'scope.writePhotosAlbum',
				            success(re) {
							  let imgUrl = that.qrCode;
				              that.saveToBlum(imgUrl);
				            }
				          })
				        }else{
				         //已授 直接调用保存到相册方法
						 let imgUrl = that.qrCode;
				          that.saveToBlum(imgUrl);
				        }
				      }
				    });
			},
			//长按保存二维码
			saveToBlum:function(imgUrl){
			    wx.downloadFile({
			      url:imgUrl,
			      success: function (res) {
			        wx.saveImageToPhotosAlbum({
			          filePath: res.tempFilePath,
			          success(result) {
			            wx.showToast({
			              title: '保存成功',
			              icon: 'success'
			            })
			          }
			        })
			      }
			    });
			  }
		}
	}
</script>

<style>
	.main{
		margin: 50% auto;
		text-align: center;
	}
</style>
